/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.zhtj.common.ResponseCode;
import com.zhuoyue.zhtj.common.ServerResponse;
import com.zhuoyue.zhtj.entity.Wxxx;
import com.zhuoyue.zhtj.service.WxxxService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Tyxx;
import com.zhuoyue.zhtj.service.TyxxService;

/**
 * 基本信息Controller
 *
 * @author hss
 * @version 2017-08-15
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/tyxx")
public class TyxxController extends BaseController {

    @Autowired
    private TyxxService tyxxService;
    @Autowired
    private WxxxService wxxxService;

    @ModelAttribute
    public Tyxx get(@RequestParam(required = false) String id) {
        Tyxx entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = tyxxService.get(id);
        }
        if (entity == null) {
            entity = new Tyxx();
        }
        return entity;
    }

    @RequiresPermissions("zhtj:tyxx:view")
    @RequestMapping(value = {"list", ""})
    public String list(Tyxx tyxx, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Tyxx> page = tyxxService.findPage(new Page<Tyxx>(request, response), tyxx);
        model.addAttribute("page", page);
        return "zhuoyue/zhtj/tyxxList";
    }

    @RequiresPermissions("zhtj:tyxx:view")
    @RequestMapping(value = "form")
    public String form(Tyxx tyxx, Model model) {
        model.addAttribute("tyxx", tyxx);
        return "zhuoyue/zhtj/tyxxForm";
    }

    @RequiresPermissions("zhtj:tyxx:edit")
    @RequestMapping(value = "save")
    public String save(Tyxx tyxx, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, tyxx)) {
            return form(tyxx, model);
        }
        tyxxService.save(tyxx);
        addMessage(redirectAttributes, "保存基本信息成功");
        return "redirect:" + Global.getAdminPath() + "/zhtj/tyxx/?repage";
    }

    @RequestMapping("register")
    public String register(Tyxx tyxx, Wxxx wxxx, HttpServletResponse response) {
        if (tyxx.getXh() == null || "".equals(tyxx.getXh()) || tyxx.getXm() == null || "".equals(tyxx)) {
            return this.renderString(response, ServerResponse.createByErrorMessage("学号或姓名不能为空"));
        }
        boolean exist = tyxxService.checkInfo(tyxx);
        if (exist) {
            return this.renderString(response, ServerResponse.createByErrorMessage("您已注册,请重新登录"));
        }
        if(wxxx.getOpenid()==null){
            return this.renderString(response, ServerResponse.createByErrorMessage("openId为空,注册失败"));
        }
        exist = wxxxService.checkOpeId(wxxx.getOpenid());
        if(exist){
            return this.renderString(response, ServerResponse.createByErrorMessage("该微信号已被绑定"));
        }
        exist = tyxxService.register(tyxx, wxxx);
        if (exist) {
            return this.renderString(response, ServerResponse.createBySuccessMessage("注册成功"));
        }
        return this.renderString(response, ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"未知错误"));
    }

    @RequiresPermissions("zhtj:tyxx:edit")
    @RequestMapping(value = "delete")
    public String delete(Tyxx tyxx, RedirectAttributes redirectAttributes) {
        tyxxService.delete(tyxx);
        addMessage(redirectAttributes, "删除基本信息成功");
        return "redirect:" + Global.getAdminPath() + "/zhtj/tyxx/?repage";
    }

    @RequestMapping(value = "checkInfo")
    public String checkInfo(Tyxx tyxx, HttpServletResponse response) {
        if (tyxx.getXh() == null || tyxx.getXm() == null || "".equals(tyxx.getXh()) || "".equals(tyxx.getXm())) {
            return renderString(response, ServerResponse.createByErrorMessage("学号或姓名不能为空"));
        }
        boolean exist = tyxxService.checkInfo(tyxx);
        if (exist) {
            return renderString(response, ServerResponse.createBySuccessMessage("认证成功"));
        }
        return renderString(response, ServerResponse.createByErrorMessage("学号或姓名有误,请重新输入"));
    }

}